package gdt.utils.security;

import java.util.List;

import gdt.dao.MemberDAO;
import gdt.entity.Member;

import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly=true)
public class JPAUserDetailsService implements UserDetailsService {

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		
		List<Member> users = dao.findByUsername(username);
		
		if(users.isEmpty())
			throw new UsernameNotFoundException(username + "not found");
		
		return users.get(0);
	}

	private MemberDAO dao;
	
	public void setDao(MemberDAO dao) {
		this.dao = dao;
	}
}
